﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyQQ
{
    public partial class Frm_Login : Form
    {
        DataOperator dataOperator = new DataOperator();
        public Frm_Login()
        {
            InitializeComponent();
        }

        private void TxtID_TextChanged(object sender, EventArgs e)
        {
            ValidateInput();
            string sql = "select Pwd,Remember,AutoLogin from tb_User where ID =" + int.Parse(TxtID.Text.Trim());
            DataSet dataSet = dataOperator.GetDataSet(sql);
            //判断用户是否存在
            if (dataSet.Tables[0].Rows.Count>0)
            {
                if (Convert.ToInt32(dataSet.Tables[0].Rows[0][1]) == 1)
                {
                    //记录密码复选框选中
                    CBoxRemember.Checked = true;
                    //自动输入密码
                    TxtPwd.Text = dataSet.Tables[0].Rows[0][0].ToString();
                    //判读是否自动登陆
                    if (Convert.ToInt32(dataSet.Tables[0].Rows[0][2]) == 1)
                    {
                        //自动登陆复选框选中
                        CBoxAutoLogin.Checked = true;
                        //自动登陆
                        PBoxClose_Click(sender, e);
                    }
                }
            }
        }

        /// <summary>
        /// 申请账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Frm_Register frm_Register = new Frm_Register();
            frm_Register.Show();
        }

        private void Frm_Login_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 登录验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateInput()
        {
            if (string.IsNullOrEmpty(TxtID.Text.Trim()))
            {
                MessageBox.Show("请输入登录账号", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                TxtID.Focus();
                return false;
            }
            else if (TxtID.Text.Length > 7)
            {
                MessageBox.Show("请输入正确的登录账号", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                TxtID.Focus();
                return false;
            }
            else if (TxtID.Text.Length > 5 && string.IsNullOrEmpty(TxtPwd.Text.Trim()))
            {
                MessageBox.Show("请输入密码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                TxtPwd.Focus();
                return false;
            }
            else
                return true;
        }

        /// <summary>
        /// 用户名输入框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtID_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果输入的是数字或者回撤或者空格
            if (char.IsDigit(e.KeyChar) || (e.KeyChar == '\r') || e.KeyChar == '\b')
            {
                //释放改事件，也就是该判断结束，不做任何操作
                e.Handled = false;
            }
            else
                //事件开启，判断生效，显示输入的数字
                e.Handled = true;
        }

        private void TxtPwd_TextChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 实现用户登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PBoxLogin_Click(object sender, EventArgs e)
        {
            if (ValidateInput())
            {
                //定义sql语句
                string sql = "select count(*) from tb_User where ID = " + int.Parse(TxtID.Text.Trim()) + "and Pwd = " + TxtPwd.Text.Trim();
                int num = dataOperator.ExecSQL(sql);
                if (num == 1)
                {
                    //设置登陆的用户号码
                    PublicClass.loginID = int.Parse(TxtID.Text.Trim());
                    //如果是记住密码
                    if (CBoxRemember.Checked)
                    {
                        //将该用户的密码状态改为记住
                        dataOperator.ExecSQLResult("update tb_User set Remember = 1 where ID = " + int.Parse(TxtID.Text.Trim()));
                        //如果为自动登陆
                        if (CBoxAutoLogin.Checked)
                        {
                            dataOperator.ExecSQLResult("update tb_User set AutoLogin = 1 where ID=" + int.Parse(TxtID.Text.Trim()));
                        }
                    }
                    else
                    {
                        dataOperator.ExecSQLResult("update tb_User set Remember = 0 where ID = " + int.Parse(TxtID.Text.Trim()));
                        dataOperator.ExecSQLResult("update tb_User set AutoLogin = 0 where ID=" + int.Parse(TxtID.Text.Trim()));

                    }
                    //在线状态
                    dataOperator.ExecSQLResult("update tb_User set Flag= 1 where ID = " + int.Parse(TxtID.Text.Trim()));
                    //创建主窗体对象
                    Frm_Main frm_Main = new Frm_Main();
                    //显示主窗体
                    frm_Main.Show();
                    //隐藏登陆主窗体
                    this.Visible = false;
                }
                else
                {
                    MessageBox.Show("输入的用户名或密码有误!", "登录提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void TxtPwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            //是否按下了回车
            if (e.KeyChar == '\r')
            {
                PBoxLogin_Click(sender, e);//使登录按钮获得鼠标焦点
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PBoxClose_Click(object sender, EventArgs e)
        {
            //退出当前应用程序
            Application.ExitThread();
        }

        private void PBoxClose_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        /// <summary>
        /// 记住密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CBoxRemember_CheckedChanged(object sender, EventArgs e)
        {
            if (!CBoxRemember.Checked)
                CBoxAutoLogin.Checked = false;
        }

        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PBoxMin_Click(object sender, EventArgs e)
        {
            //最小化窗口
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
